package com.jse.dao;

import java.util.List;

import javax.sql.DataSource;

import org.nutz.dao.Condition;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.github.benmanes.caffeine.cache.LoadingCache;

public class Dao extends NutDao {

	public Dao(DataSource dataSource) {
		super(dataSource);
	}
	private LoadingCache<Object, Object> cache;

	public void setCache(LoadingCache<Object, Object> cache) {
		this.cache = cache;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Record> query(String tableName, Condition cnd) {
		if(cnd==null||cnd.toString().equals(" WHERE 1=1")&&cnd.toString().startsWith(" WHERE 1=1 ORDER")) {
			if(cache.get(tableName)!=null) {
				return (List<Record>) cache.get(tableName);
			}else {
				List<Record> list=super.query(tableName, cnd);
				cache.put(tableName, list);
			}
		}
		return super.query(tableName, cnd);
	}

	
}
